<template>
  <div class="table-view">
    <GroundWaveForm v-if="caseType === 17" :tableList="tableList" @updateList="updateList" />
    <XcTable
      class="is-list"
      :class="{ 'ground-wave-table': caseType === 17 }"
      ref="tableView"
      :isQuickEditable="testStatus === 1"
      :quickEditable="{ deleteDisabled: true, addDisabled: true }"
      :key="caseName"
      :tableKey="caseName"
      :config="resultJson.config"
      :values="JSON.parse(JSON.stringify(showTableData))"
      :indexColumn="resultJson.indexColumn"
      :spanMethod="resultJson.spanMethod"></XcTable>
  </div>
</template>

<script lang="ts" setup>
import { ref, nextTick, computed } from "vue";
import type { TestResultItem, GroundDirectionAccuracies } from "@api/test/index.d";
import useConfig from "../../hooks/useConfig";
import GroundWaveForm from "./components/GroundWaveForm.vue";
import { useRoute } from "vue-router";
const route = useRoute();

defineOptions({
  name: "TableBox",
  components: {}
});
const props = withDefaults(defineProps<{ caseName?: string; tableList?: TestResultItem[]; treeRootName: string; caseType: number; testStatus: number }>(), {
  caseName: "",
  tableList: () => [],
  treeRootName: "",
  caseType: 0,
  testStatus: 1
});
const tableView = ref();
const groundWaveTableData = ref<GroundDirectionAccuracies[]>([]);

const resultJson = computed(() => {
  const json = useConfig({ taskId: String(route.query.taskId || "") }).getJson(props.caseName, props.tableList, props.treeRootName);
  json.config?.forEach((it) => {
    if (["input", "datePicker"].includes(String(it.editType))) {
      it.type = props.testStatus !== 1 ? "text" : "editable";
    }
  });
  return json;
});
const showTableData = computed(() => {
  return props.caseType === 17 ? groundWaveTableData.value : resultJson.value.data || props.tableList;
});

const scrollBottom = (isReset = false) => {
  nextTick(() => {
    const table = tableView.value.table.layout.table.refs;
    const ele = table.bodyWrapper.firstElementChild.firstElementChild;
    ele.scrollTop = isReset ? 0 : ele.scrollHeight;
  });
};
const updateList = (list: GroundDirectionAccuracies[]) => {
  groundWaveTableData.value = list || [];
};
defineExpose({ scrollBottom });
</script>
<style scoped lang="scss">
.table-view {
  height: 100%;
}
.is-list {
  &.ground-wave-table {
    height: calc(100% - 100px);
  }
}
:deep(.el-table) {
  border: 0;
  font-size: 12px;
  .cell {
    height: 28px;
    line-height: 28px !important;
  }
  .el-table__cell {
    padding: 0;
  }
  .el-table__header {
    th {
      background-color: #f4f4f4 !important;
    }
  }
}
</style>
../../hooks/index
